window.onload = loadTable();
layui.use(['form', 'layer'], function () {
    var form = layui.form
        , layer = layui.layer;
    var $ = layui.jquery;
    form.on('submit(address)', function (data) {
        var jsonData = JSON.stringify(data.field);
        console.log(jsonData)
        $.ajax({
            url: basePath + "/user/insertAdd"
            , data: jsonData
            , contentType: "application/json;charset=UTF-8"
            , type: "put"
            , dataType: "json"
            , beforeSend: function () {
                layer.load(1, {
                    content: '添加中...',
                    success: function (layero) {
                        layero.find('.layui-layer-content').css({
                            'padding-top': '39px',
                            'width': '60px'
                        });
                    }
                });
            }, complete: function () {
                layer.closeAll('loading');
            }, success: function (data) {
                if (data.status == 200) {
                    layer.msg(data.message, {
                        time: 1000,
                        icon: 1,
                        offset: '100px'
                    }, function () {
                        location.reload()
                    })
                } else if (data.status == 202) {
                    layer.msg(data.message, {
                        time: 1000,
                        icon: 2,
                        offset: '100px'
                    }).then(function () {
                        this.window.location.href = basePath + "/login"
                    })
                } else if (data.status == 201) {
                    layer.msg(data.message, {
                        time: 1000,
                        icon: 2,
                        offset: '100px'
                    });
                }
            }, error: function () {
                layer.msg("系统错误", {
                    time: 1000,
                    icon: 2,
                    offset: '100px'
                });
            }
        })
    })
})

function loadTable() {
    layui.use(['element', 'table',], function () {
        var table = layui.table;
        var $ = layui.jquery;
        table.render({
            elem: '#addrList'
            , url: basePath + '/user/getAddrList'
            , cols: [
                [
                    {field: 'id', title: 'ID', width: 80, align: 'center', rowspan: 2}
                    , {field: 'name', title: '收货人', width: 80, align: 'center', rowspan: 2}
                    , {field: 'phone', title: '联系电话', width: 100, align: 'center', rowspan: 2}
                    , {title: '收货地址', align: 'center', colspan: 4}
                    , {title: '操作', width: 250, align: 'center', rowspan: 2,sort: true,fixed: 'right',toolbar: '#barDemo' }

                ],
                [
                    {field: 'province', title: '地址', width: 80, align: 'center'}
                    , {field: 'town', title: '地址', width: 80, align: 'center'}
                    , {field: 'county', title: '地址', width: 80, align: 'center'}
                    , {field: 'detail', title: '详细地址', width: 150, align: 'center'}
                ]
            ], done: function (res, curr, count) {
                var i=1;
                $("[data-field='id']").children().each(function () {
                    if($(this).text() == 'ID') {
                        $(this).text("ID")
                    }else{
                        $(this).text(i++)
                    }
                });
            }
        });

        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                console.log(data)
                layui.form.render('select')
                $('#id').val(data.id)
                $('#name').val(data.name)
                $('#phone').val(data.phone)
                $('#province').append('<option value="' + data.province + '" selected>' + data.province + '</option>')
                $('#town').append('<option value="' + data.town + '" selected>' + data.town + '</option>')
                $('#county').append('<option value="' + data.county + '" selected>' + data.county + '</option>')
                $('#detail').val(data.detail)
                layui.form.render();
                return;
            }
            /*删除*/
            if (obj.event === 'del') {
                layer.confirm('确认删除地址吗？', {
                    btn: ['确定', '算了'], //按钮
                    title: "删除商品",
                    offset: "50px"
                }, function () {
                    layer.closeAll();
                    $.ajax({
                        url: basePath + '/user/delById'
                        , data: {id: data.id}
                        , dataType: 'json'
                        , method: 'get'
                        , beforeSend: function () {
                            layer.load(1, { //icon支持传入0-2
                                content: '请稍等...',
                                success: function (layero) {
                                    layero.find('.layui-layer-content').css({
                                        'padding-top': '39px',
                                        'width': '60px'
                                    });
                                }
                            });
                        },
                        complete: function () {
                            layer.closeAll('loading');
                        },
                        success: function (data) {
                            if (data.status === 200) {
                                layer.msg(data.message, {
                                    time: 1000,
                                    icon: 1,
                                    offset: '50px'
                                }, function () {
                                    location.reload();
                                });
                            } else {
                                layer.msg(data.message, {
                                    time: 1000,
                                    icon: 2,
                                    offset: '50px'
                                });
                            }
                        }
                    })
                    return;
                });
            }

            if (obj.event === "setDefault") {
                $.ajax({
                    url: basePath + '/user/setDefault'
                    , data: {id: data.id}
                    , dataType: 'json'
                    , method: 'get'
                    , beforeSend: function () {
                        layer.load(1, { //icon支持传入0-2
                            content: '请稍等...',
                            success: function (layero) {
                                layero.find('.layui-layer-content').css({
                                    'padding-top': '39px',
                                    'width': '60px'
                                });
                            }
                        });
                    },
                    complete: function () {
                        layer.closeAll('loading');
                    },
                    success: function (data) {
                        if (data.status === 200) {
                            layer.msg(data.message, {
                                time: 1000,
                                icon: 1,
                                offset: '50px'
                            }, function () {
                                location.reload();
                            });
                        } else {
                            layer.msg(data.message, {
                                time: 1000,
                                icon: 2,
                                offset: '50px'
                            });
                        }
                    }
                })
                return;
            }

        })
    })
}
